﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using V5_BLL;
using V5_Model;

namespace V5_EShop._Template.Default
{
    public partial class Register : BasicPage
    {
        private string returnUrl = "";

        private void InitializeComponent()
        {
            this.btnRegSave.Click += new EventHandler(btnRegSave_Click);
        }

        void btnRegSave_Click(object sender, EventArgs e)
        {
            this.Bind_UserReg();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            returnUrl = Input.SetString(Request.QueryString["returnurl"]);
            if (returnUrl == "")
            {
                returnUrl = "login.aspx";
            }
        }

        private void Bind_UserReg()
        {
            string action = Input.SetString(this.hfAction.Value);
            if (action == "reg")
            {
                string txtUserName = Input.SetString(this.txtUserName.Text);
                string txtUserPwd = Input.SetString(this.txtUserPwd.Text);
                string txtUserPwd2 = Input.SetString(this.txtUserPwd2.Text);
                string txtUserEmail = Input.SetString(this.txtUserEmail.Text);
                string txtVerCode = Input.SetString(this.txtVerCode.Text);
                BLLUser bll = new BLLUser();
                ModelUser model = new ModelUser();
                if (txtUserName == "")
                {
                    Js.AlertAndGoHistory("用户名不能为空!", -1);
                    return;
                }
                if (txtUserPwd == "")
                {
                    Js.AlertAndGoHistory("用户密码不能为空!", -1);
                    return;
                }
                if (txtUserPwd2 == "")
                {
                    Js.AlertAndGoHistory("用户确认密码不能为空!", -1);
                    return;
                }
                if (txtUserPwd.Trim() != txtUserPwd2.Trim())
                {
                    Js.AlertAndGoHistory("两次输入的密码不相同", -1);
                    return;
                }
                if (txtUserEmail == "")
                {
                    Js.AlertAndGoHistory("用户Email不能为空!", -1);
                    return;
                }
                if (txtVerCode == "")
                {
                    Js.AlertAndGoHistory("验证码不能为空!", -1);
                    return;
                }
                if (txtVerCode != Input.SetString(Session["LoginCode"]))
                {
                    Js.AlertAndGoHistory("验证码输入不正确!", -1);
                    return;
                }

                if (!bll.CheckRegUser(txtUserName))
                {
                    Js.AlertAndGoHistory("该用户已经存在!请换一个试一试!", -1);
                    return;
                }

                model.ID = bll.GetMaxId();
                model.UserName = txtUserName;
                model.UserPassword = Input.MD5(txtUserPwd, 16);
                model.UserEmail = txtUserEmail;
                model.RegDate = DateTime.Now;
                model.LoginDate = DateTime.Now;
                bll.Add(model);
                Response.Redirect("/login.aspx");
            }
            return;
        }

    }
}
